import { _decorator, Component, Label, Node } from 'cc';
import { BaseComponent } from '../../Manager/UIManager/BaseComponent';
import { GAME_POP, GlobalData, SOUND_TYPE } from '../../Data/GlobalData';
import { Tools } from '../../Tool/Tools';
import { DiffMajiang } from './DiffMajiang';
const { ccclass, property } = _decorator;

@ccclass('DiffGame')
export class DiffGame extends BaseComponent {

    //游戏关卡
    @property(Label)
    public LevelLabel: Label = null;

    //时间文本
    @property(Label)
    public TimeLabel: Label = null;

    //游戏时间
    private GameTime: number = 10 * 60;

    //麻将层
    @property(Node)
    private DiffMajiang: Node = null;

    //进入游戏得时间
    private GameStartTime: number = 0;

    //麻将数量
    private MajiangNum: number[] = [22, 30, 40, 50];

    protected OnShow(): void {

        //
        this.GameStart();

    }

    /**
     * 游戏开始
     */
    public GameStart() {
        GlobalData.GameOver = false;
        this.LevelLabel.string = (app.gameData.GameData.currentlevel + 1).toString();
        this.TimeLabel.string = Tools.secondsFormat(this.GameTime, false);
        //
        let majiang_num: number = this.MajiangNum[Math.min(app.gameData.GameData.currentlevel, this.MajiangNum.length - 1)]

        console.log(majiang_num, "创建麻将数量")

        this.DiffMajiang.getComponent(DiffMajiang).UpdateLevel(majiang_num)

        this.GameStartTime = Date.now();

    }

    /**
     * 
     */
    protected update(dt: number): void {

        if (GlobalData.GameOver) return;

        let count_time: number = this.GameTime - Math.floor((Date.now() - this.GameStartTime) / 1000);
        this.TimeLabel.string = Tools.secondsFormat(count_time, false)


        if (count_time <= 0) {
            GlobalData.GameOver = true;
            app.popManager.ShowPop(GAME_POP.PopEnd);
        }

    }


    /**
        * 点击设置按钮
        */
    public SetClick() {
        app.audioManager.PlayEffect(SOUND_TYPE.Button);
        app.popManager.ShowPop(GAME_POP.PopSet);
    }

}


